<?php
/**
 * @title 100189. 找出网格的区域平均强度
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

class Solution {

    /**
     * @param Integer[][] $image
     * @param Integer $threshold
     * @return Integer[][]
     */
    function resultGrid($image, $threshold) {
        /**
         * @since 2024-02-04 像素区域的起点
         */
        $map = [];

        /**
         *     x1 x1 x1
         *  x2 x1 x1 x1
         *  x2 x1 x1 x1
         *  x2 x2 x2 ?
         *
         * @since 2024-02-04 image的n行，m列
         */
        $n = count($image);
        $m = count($image[0]);
        for($r=0; $r<$n; $r++){
            for($c=0; $c<$m; $c++){
                if(isset($map[$r][$c])){
                } else {
                    /**
                     * @since 2024-02-04 行和高不足支撑3
                     */
                    if($n-$r<=1 || $m-$c<=1){
                        $map[$r][$c] = 0;
                    } else {
                        $nodeArray = [
                            [$r, $c, $r, $c+1],
                            [$r, $c+1, $r, $c+2],
                            [$r+1, $c, $r+1, $c+1],
                            [$r+1, $c+1, $r+1, $c+2],
                            [$r+2, $c, $r+2, $c+1],
                            [$r+2, $c+1, $r+2, $c+2],

                            [$r, $c, $r+1, $c],
                            [$r, $c+1, $r+1, $c+1],
                            [$r, $c+2, $r+1, $c+2],
                            [$r+1, $c, $r+2, $c],
                            [$r+1, $c+1, $r+2, $c+1],
                            [$r+1, $c+2, $r+2, $c+2],
                        ];

                        $map[$r][$c] = 1;
                        foreach ($nodeArray as list($a, $b, $c, $d)){
                            if(abs($image[$a][$b] - $image[$c][$d]) > $threshold){
                                $map[$r][$c] = 0;
                                break;
                            } else {}
                        }
                    }
                }
            }
        }
    }
}


/**
 * @url http://127.0.0.1/leetcode/202402/2024.02.04_6.php
 */
$datas = [

];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    /**
     * @since 2024-01-15 超时和错误案例参数
     */
    // $data = $xhprof->FormatArgs("/tmp/leetcode/xxx-xxx.txt");

    // $data[0] = $xhprof->listNode($data[0]);
    // $data[0] = $xhprof->tree($data[0]);
    echo $xhprof->DumpArgs($data);
    echo "\n<hr>\n";

    $obj = new Solution();
    $result = $obj->(...$data);
    echo $xhprof->DumpArgs([$result]);
    // var_dump($result);

    echo "\n<hr>". str_repeat("<br>", 2) . str_repeat(PHP_EOL, 5);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
